package cn.iocoder.yudao.module.enterprise.service.reductionscene;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.reductionscene.vo.ReductionSceneCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.reductionscene.vo.ReductionScenePageReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.reductionscene.vo.ReductionSceneUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.reductionscene.ReductionSceneConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.reductionscene.ReductionSceneDO;
import cn.iocoder.yudao.module.enterprise.dal.mysql.reductionscene.ReductionSceneMapper;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.enterprise.enums.ErrorCodeConstants.REDUCTION_SCENE_NOT_EXISTS;

/**
 * 减排场景 Service 实现类
 *
 * @author StevenShen
 */
@Service
@Validated
public class ReductionSceneServiceImpl implements ReductionSceneService {

    @Resource
    private AdminUserApi adminUserApi;

    @Resource
    private ReductionSceneMapper reductionSceneMapper;

    @Override
    public Long createReductionScene(ReductionSceneCreateReqVO createReqVO) {
        // 插入
        ReductionSceneDO reductionScene = ReductionSceneConvert.INSTANCE.convert(createReqVO);
        reductionSceneMapper.insert(reductionScene);
        // 返回
        return reductionScene.getId();
    }

    @Override
    public void updateReductionScene(ReductionSceneUpdateReqVO updateReqVO) {
        // 校验存在
        validateReductionSceneExists(updateReqVO.getId());
        // 更新
        ReductionSceneDO updateObj = ReductionSceneConvert.INSTANCE.convert(updateReqVO);
        reductionSceneMapper.updateById(updateObj);
    }

    @Override
    public void deleteReductionScene(Long id) {
        // 校验存在
        validateReductionSceneExists(id);
        // 删除
        reductionSceneMapper.deleteById(id);
    }

    private void validateReductionSceneExists(Long id) {
        if (reductionSceneMapper.selectById(id) == null) {
            throw exception(REDUCTION_SCENE_NOT_EXISTS);
        }
    }

    @Override
    public ReductionSceneDO getReductionScene(Long id) {
        return reductionSceneMapper.selectById(id);
    }

    @Override
    public PageResult<ReductionSceneDO> getReductionScenePage(ReductionScenePageReqVO pageReqVO) {
        PageResult<ReductionSceneDO> pageResult = reductionSceneMapper.selectPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return new PageResult<>(pageResult.getTotal());
        }

        List<ReductionSceneDO> list = pageResult.getList();
        list = list.stream().map(e -> {
            AdminUserRespDTO user = adminUserApi.getUser(Long.parseLong(e.getUpdater())).getCheckedData();
            // 查询更新人
            e.setUpdater(user.getNickname());
            return e;
        }).collect(Collectors.toList());

        return new PageResult<>(list, pageResult.getTotal());
    }

    @Override
    public List<ReductionSceneDO> getReductionSceneList(Collection<Long> ids) {
        return reductionSceneMapper.selectBatchIds(ids);
    }

}
